package cc.rengu.redp.system.controller;

import cc.rengu.redp.base.domain.router.VueRouter;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.utils.EasyExcelUtil;
import cc.rengu.redp.system.domain.Menu;
import cc.rengu.redp.system.manager.UserManager;
import cc.rengu.redp.system.service.MenuService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Autowired
    private UserManager userManager;
    @Autowired
    private MenuService menuService;

    @GetMapping("/{username}")
    public ArrayList<VueRouter<Menu>> getUserRouters(@NotBlank(message = "{required}") @PathVariable String username) {
        return this.userManager.getUserRouters(username);
    }

    @GetMapping
    @RequiresPermissions("menu:view")
    public Map<String, Object> menuList(Menu menu) {
        return this.menuService.findMenus(menu);
    }

    @Log("新增菜单/按钮")
    @PostMapping
    @RequiresPermissions("menu:add")
    public RedpResponse addMenu(@Valid Menu menu) {
        try {
            this.menuService.createMenu(menu);
        } catch (Exception e) {
            String message = "新增菜单/按钮失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("新增菜单/按钮成功");
    }

    @Log("删除菜单/按钮")
    @DeleteMapping("/{menuIds}")
    @RequiresPermissions("menu:delete")
    public RedpResponse deleteMenus(@NotBlank(message = "{required}") @PathVariable String menuIds) {
        try {
            String[] ids = menuIds.split(StringPool.COMMA);
            this.menuService.deleteMeuns(ids);
        } catch (Exception e) {
            String message = "删除菜单/按钮失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("删除菜单/按钮成功");
    }

    @Log("修改菜单/按钮")
    @PutMapping
    @RequiresPermissions("menu:update")
    public RedpResponse updateMenu(@Valid Menu menu) {
        try {
            this.menuService.updateMenu(menu);
        } catch (Exception e) {
            String message = "修改菜单/按钮失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("删除菜单/按钮成功");
    }

    @Log("导出菜单")
    @PostMapping("excel")
    @RequiresPermissions("menu:export")
    public RedpResponse export(Menu menu, HttpServletResponse response) {
        try {
            List<Menu> menus = this.menuService.findMenuList(menu);
            EasyExcelUtil.export(response, menus, "菜单信息表");
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("导出Excel成功");
    }
}
